<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const data = {
            a: {
                b: 1,
                c: 2,
                d: { e: 5 }
            },
            b: [1, 3, { a: 2, b: 3 }],
            c: 3
        }
        function flatten(obj) {
            if (!isObject(obj)) {
                return;
            }
            let res = {};
            const dfs = (cur, prefix) => {
                if (isObject(cur)) {
                    if (Array.isArray(cur)) {
                        cur.forEach((item, index) => {
                            dfs(item, `${prefix}[${index}]`);
                        });
                    } else {
                        for (let k in cur) {
                            dfs(cur[k], `${prefix}${prefix ? "." : ""}${k}`);
                        }
                    }
                } else {
                    res[prefix] = cur;
                }
            };
            dfs(obj, "");

            return res;
        }
        /**
         * 这个 isObject 函数省略 function 类型的判断
         * see: https://github.com/lodash/lodash/blob/master/isObject.js
        */
        function isObject(val) {
            return typeof val === "object" && val !== null;
        }
        const res = flatten(data);
        console.log(res);
    </script>
</body>

</html>